package com.xssweibo.ui;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.sso.SsoHandler;
import com.xssweibo.service.BaseService;
import com.xssweibo.service.Task;

public class Login extends Activity implements IWeiboActivity {

	private SsoHandler mSsoHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		BaseService.addActivity(this);

		mSsoHandler = new SsoHandler(Login.this, BaseService.mWeibo);
		mSsoHandler.authorize(new AuthDialogListener());
	}

	@Override
	protected void onResume() {
		super.onResume();
		init();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		/**
		 * 下面两个注释掉的代码，仅当sdk支持sso时有效，
		 */
		if (mSsoHandler != null) {
			mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}

	class AuthDialogListener implements WeiboAuthListener {
		@Override
		public void onComplete(Bundle values) {
			String token = values.getString("access_token");
			String expires_in = values.getString("expires_in");
			String uid = values.getString("uid");
			Oauth2AccessToken accessToken = new Oauth2AccessToken(token,
					expires_in);
			if (accessToken.isSessionValid()) {
				BaseService.saveUser(uid, accessToken);
				Map map = new HashMap();
				map.put("uid", uid);
				Task task = new Task(Task.TASK_LOGIN, map);
				BaseService.addTask(task);

				String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
						.format(new java.util.Date(accessToken.getExpiresTime()));
				Toast.makeText(
						getApplicationContext(),
						"认证成功: \r\n access_token: " + token + "\r\n"
								+ "expires_in: " + expires_in + "\r\n有效期："
								+ date, Toast.LENGTH_SHORT).show();

				// 切换到home页面
				Intent it = new Intent(Login.this, Main.class);
				startActivity(it);
				finish();
			}
		}

		@Override
		public void onError(WeiboDialogError e) {
			Toast.makeText(getApplicationContext(),
					"Auth error : " + e.getMessage(), Toast.LENGTH_LONG).show();
		}

		@Override
		public void onWeiboException(WeiboException e) {
			Toast.makeText(getApplicationContext(),
					"Auth Exception : " + e.getMessage(), Toast.LENGTH_LONG)
					.show();
		}

		@Override
		public void onCancel() {
			Toast.makeText(getApplicationContext(), "登陆取消", Toast.LENGTH_SHORT)
					.show();
		}
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		Intent it = new Intent("com.xssweibo.service.BaseService");
		startService(it);
	}

	@Override
	public void refresh(Object... args) {
		// TODO Auto-generated method stub
		
	}

}
